package com.uobgroup.ews.uam.core.domain;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

import com.uobgroup.ews.uam.core.dao.ReferenceTypeDao;
import com.uobgroup.ews.uam.core.service.ReferenceTypeService;

@Component
@Configurable
public class ReferenceTypeDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<ReferenceType> data;

	@Autowired
    ReferenceTypeService referenceTypeService;

	@Autowired
    ReferenceTypeDao referenceTypeDao;

	public ReferenceType getNewTransientReferenceType(int index) {
        ReferenceType obj = new ReferenceType();
        setCode(obj, index);
        setName(obj, index);
        return obj;
    }

	public void setCode(ReferenceType obj, int index) {
        String code = "code_" + index;
        if (code.length() > 64) {
            code = code.substring(0, 64);
        }
        obj.setCode(code);
    }

	public void setName(ReferenceType obj, int index) {
        String name = "name_" + index;
        if (name.length() > 255) {
            name = name.substring(0, 255);
        }
        obj.setName(name);
    }

	public ReferenceType getSpecificReferenceType(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        ReferenceType obj = data.get(index);
        Long id = obj.getId();
        return referenceTypeService.findReferenceType(id);
    }

	public ReferenceType getRandomReferenceType() {
        init();
        ReferenceType obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return referenceTypeService.findReferenceType(id);
    }

	public boolean modifyReferenceType(ReferenceType obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = referenceTypeService.findReferenceTypesByApplicationScope(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'Reference Type' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<ReferenceType>();
        for (int i = 0; i < 10; i++) {
            ReferenceType obj = getNewTransientReferenceType(i);
            try {
                referenceTypeService.saveReferenceType(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            referenceTypeDao.flush();
            data.add(obj);
        }
    }
}
